package leetcode.每日一题;

import leetcode.剑指offer.pojo.TreeNode;
import org.junit.Test;

import java.util.*;

/**
 * @author ：zsy
 * @date ：Created 2022/3/21 14:32
 * @description：https://leetcode-cn.com/problems/two-sum-iv-input-is-a-bst/
 */
public class 两数之和IV_输入BST {
    @Test
    public void test() {
        Solution solution = new Solution();
        TreeNode root = new TreeNode(5);
        root.left = new TreeNode(3, new TreeNode(2), new TreeNode(4));
        root.right = new TreeNode(6, null, new TreeNode(7));
        System.out.println(solution.findTarget(root, 28));
    }

    class Solution {
        HashSet<Integer> set = new HashSet<>();
        public boolean findTarget(TreeNode root, int k) {
            if (root == null) return false;
            if (!set.add(root.val)) {
                return true;
            }
            set.add(k - root.val);
            return findTarget(root.left, k) || findTarget(root.right, k);
        }
    }
}
